package com.myrpc.common.sessionchannel;

import java.util.HashMap;
import java.util.concurrent.SynchronousQueue;

import com.alibaba.fastjson.JSON;
import com.myrpc.common.vo.Request;

import io.netty.channel.Channel;

public class Session {

	private Channel channel;

	private String className;

	private Object obj;

	public static HashMap<String, SynchronousQueue<Object>> queueMap = new HashMap<String, SynchronousQueue<Object>>();

	public Object clientSend(Request request) throws InterruptedException {

		if (channel.isActive()) {
			SynchronousQueue<Object> queue = new SynchronousQueue<Object>();
			queueMap.put(request.getId(), queue);
			
			channel.writeAndFlush(JSON.toJSONString(request));
			Object object = queue.take();
			return object;
		}

		return null;
	}

	public Session(Channel channel, String className, Object serverId) {
		super();
		this.channel = channel;
		this.className = className;
		this.obj = serverId;
	}

	public void writeAndFlush(String msg) {
		channel.writeAndFlush(msg);
	}

	public void close() {
		channel.close();
	}

	public Channel getChannel() {
		return channel;
	}

	public void setChannel(Channel channel) {
		this.channel = channel;
	}

	public String getClassName() {
		return className;
	}

	public void setClassName(String className) {
		this.className = className;
	}

	public Object getObj() {
		return obj;
	}

	public void setObj(Object obj) {
		this.obj = obj;
	}

}
